package com.dmtt.web.service.impl;

import com.dmtt.web.entity.CallPolice;
import com.dmtt.web.mapper.CarAlertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CarAlertServiceImpl {

    @Autowired
    private CarAlertMapper carAlertMapper;


    //报警信息存储
    public List<CallPolice> callPolice(){
        List<CallPolice> callPolices = carAlertMapper.agvInfo();
        Calendar calendar1=Calendar.getInstance();
        calendar1.setTime(new Date());
        calendar1.add(Calendar.HOUR_OF_DAY,0);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat.format(calendar1.getTime());
        if (callPolices.size() != 0) {
            for (CallPolice callPolice : callPolices) {
                Integer aaa = carAlertMapper.intCall(callPolice.getAgvName());
                CallPolice callPolices1 = carAlertMapper.callfind2(callPolice.getAgvName(),1,callPolice.getErrName());
                if (callPolices1 != null){
                    carAlertMapper.updateAlert(callPolices1.getId(),2);
                }
                 if (aaa == 0 && !callPolice.getErrName().equals("工作正常") && !callPolice.getErrName().equals("单机") &&
                         !callPolice.getErrName().equals("无任务等待") && !callPolice.getErrName().equals("关机") &&
                         !callPolice.getErrName().equals("空车行驶") && !callPolice.getErrName().equals("装卸货等待")){
                    callPolice.setCallStatus(1);
                    callPolice.setStartTime(startTime);
                    carAlertMapper.saveCall(callPolice);
                }
            }
        } else {
            List<CallPolice> callPolices1 = carAlertMapper.callfind1();
            for (CallPolice callPolice : callPolices1) {
                carAlertMapper.updateAlert(callPolice.getId(),2);
            }
        }
        List<CallPolice> callPoliceList = carAlertMapper.findCallPolice(startTime,1);
        return callPoliceList;
    }

}
